package com.rjet.view_demo1;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.WindowManager;

import java.util.ArrayList;

public class Main2Activity extends AppCompatActivity {

    ArrayList<DemoEntity> demoEntities = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        //隐藏系统自带导航栏
        // 隐藏系统导航栏和状态栏
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            // 透明状态栏
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//            // 透明导航栏
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
//        }

//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            // 沉浸式全屏模式
//            getWindow().getDecorView().setSystemUiVisibility(
//                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
//                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
//                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // 隐藏导航栏
//                            | View.SYSTEM_UI_FLAG_FULLSCREEN // 隐藏状态栏
//            );
//        }

        for (int i = 0; i < 100; i++) {
            DemoEntity demoEntity = DemoEntity.builder().name("测试商品" + (i + 1))
                    .id(i)
                    .content("1")
                    .selected(0)
                    .age((i + 1))
                    .build();
            demoEntities.add(demoEntity);
        }

        RecyclerView rv = (RecyclerView)findViewById(R.id.news_list_recyclerView2);
        Demo2Adapter demoAdapter = new Demo2Adapter(this, R.layout.demo_layout2, demoEntities);
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setAdapter(demoAdapter);
        demoAdapter.setNewData(demoEntities);

        demoAdapter.setChooseListener(new Demo2Adapter.ChooseListener() {
            @Override
            public void choose(Integer index) {

                index = index-101;
                if (demoEntities.get(index).getSelected() == 0){
                    demoEntities.get(index).setSelected(1);
                }else {
                    demoEntities.get(index).setSelected(0);
                }

                new Handler().post(new Runnable() {
                    @Override
                    public void run() {

                        demoAdapter.notifyDataSetChanged();
                    }
                });
            }

            @Override
            public void finishEdit(String num, Integer tag) {

                demoEntities.get(tag).setContent(num);
                demoAdapter.notifyDataSetChanged();
            }

            @Override
            public void minus(Integer tag) {
                Integer num = Integer.valueOf(demoEntities.get(tag).getContent());
                num--;
                demoEntities.get(tag).setContent(num+"");
                demoAdapter.notifyDataSetChanged();
            }

            @Override
            public void plus(Integer tag) {
                Integer num = Integer.valueOf(demoEntities.get(tag).getContent());
                num++;
                demoEntities.get(tag).setContent(num+"");
                demoAdapter.notifyDataSetChanged();
            }
        });
    }
}
